package org.deltaset.samples.petclinic.visit;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.inject.Inject;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import org.deltaset.meta.annotation.DsCustom;
import org.deltaset.meta.annotation.DsLabel;
import org.deltaset.meta.annotation.DsOrder;
import org.deltaset.meta.annotation.DsRepository;
import org.deltaset.meta.annotation.DsView;
import org.deltaset.meta.annotation.DsViewType;
import org.deltaset.meta.annotation.DsViews;
import org.springframework.beans.factory.annotation.Configurable;

@DsViews({
    @DsView(type = DsViewType.LIST, order = {"name", "weight"})
})
@DsRepository({AnimalRepository.class, AnimalRepositoryAddon.class})
@Entity
@Configurable
public class Animal implements Serializable {

    private static final long serialVersionUID = -6400134913880546720L;
    @Id
    private String id;// = UUID.randomUUID().toString();
    private String name;
    @DsCustom(value = "textarea", params = {"cols=30", "rows=5"})
    private String origin;
    private Double weight;
    @ManyToOne
    private Person owner;
    @Transient
    @Inject
    private AnimalRepository animalRepository;

    @DsOrder("2.2")
    public Animal saveMe(@DsLabel("the_name") String name) {
        this.setName(name);
        animalRepository.save(this);
        return new Animal();
    }

    public boolean hideSaveMe() {
        return null == this.getId();
    }

    @PrePersist
    private void prePersist() {
        id = UUID.randomUUID().toString();
    }

    public String title() {
        return getName() + " " + getWeight();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getOwner() {
        return owner;
    }

    public void setOwner(Person owner) {
        this.owner = owner;
    }

    public Set<Person> findSome() {
        final Set<Person> personSet = new HashSet<Person>();
        for (int i = 0; i < 6; i++) {
            final Person person = new Person();
            person.setId(new Long(i));
            person.setFamilyName(" transient " + i);
            person.setFirstName(" liolio " + i);
            personSet.add(person);
        }
        return personSet;
    }

    public String sayHi(String name) {
        return this.name + " says 'hi' to " + name;
    }

    public int numberOfSome() {
        return findSome().size();
    }

    public Person ownerDetail() {
        return owner;
    }
}
